package com.hailiang.study.rmi;

import com.sun.jndi.rmi.registry.ReferenceWrapper;

import javax.naming.NamingException;
import javax.naming.Reference;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * @author hailiang.jiang
 * @date 2021年12月11 11:15
 */
public class RMIServer {
    public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(1099);
            Registry registry = LocateRegistry.getRegistry();
            System.out.println("Create RMI Registry on port 1099");
            Reference reference = new Reference("com.hailiang.study.rmi.EvilObj", "com.hailiang.study.rmi.EvilObj", null);
            // ReferenceWrapper包裹Reference类，使其能够通过RMI进行远程访问
            ReferenceWrapper refObjWrapper = new ReferenceWrapper(reference);
            registry.bind("evil", refObjWrapper);
        } catch (RemoteException | AlreadyBoundException | NamingException e) {
            e.printStackTrace();
        }
    }
}
